﻿using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace UI.Controllers
{
    [AllowAnonymous]
    public class HuiZhongPayController : Controller
    {
        // GET: HuiZhongPay
        public ActionResult ProcessPay()
        {
            string shop_id = "2429";//商户ID号
            string bank_Type = "1004";
            decimal amount = Convert.ToDecimal(11);
            string orderid = Guid.NewGuid().ToString().Substring(0, 20).Replace("-", ""); //订单号
            string callBackurl = "http://xkpay.twwvd.cn/HuiZhongPay/ProcessNotify";//回调
            string key = "a65e72cd24ad4509b0ecdadda6ecb9a4";
            string posturl = "api.runjiana.cn/chargebank.aspx";
            String param = String.Format("parter={0}&type={1}&value={2}&orderid={3}&callbackurl={4}", shop_id, bank_Type, amount, orderid, callBackurl);
            String PostUrl = String.Format("http://" + posturl + "?{0}&sign={1}", param, FormsAuthentication.HashPasswordForStoringInConfigFile(param + key, "MD5").ToLower());
            //return Redirect(PostUrl);//转发URL地址 
            //Response.Redirect(posturl);
            //return View();
            return Redirect(PostUrl);
        }
        public ContentResult ProcessNotify()
        {
            String key = "a65e72cd24ad4509b0ecdadda6ecb9a4";//配置文件密钥
                                                                     //返回参数
            String orderid = Request["orderid"];//返回订单号
            String opstate = Request["opstate"];//返回处理结果
            String ovalue = Request["ovalue"];//返回实际充值金额
            String sign = Request["sign"];//返回签名
            String sysorferid = Request["sysorferid"];//录入时产生流水号。
            String completiontime = Request["completiontime"];//处理时间。
            String attach = Request["attach"];//上行附加信息
            String msg = Request["msg"];//返回订单处理消息

            String param = String.Format("orderid={0}&opstate={1}&ovalue={2}{3}", orderid, opstate, ovalue, key);//组织参数
            LogHelper.Debug(opstate);                                                                                        //比对签名是否有效
            if (sign.Equals(FormsAuthentication.HashPasswordForStoringInConfigFile(param, "MD5").ToLower()))
            {
                //执行操作方法
                //执行操作方法
                if (opstate == "0")
                {
                    Response.Write("opstate=0");//支付成功,请自行处理订单信息
                }
                else if (opstate == "-1")
                {
                    //卡号密码错误
                }
                else if (opstate == "-2")
                {
                    //卡实际面值和提交时面值不符，卡内实际面值未使用
                }
                else if (opstate == "-4")
                {
                    //卡在提交之前已经被使用
                }
                else if (opstate == "-5")
                {
                    //失败，原因请查看msg
                }
            }
            else
            {
                Response.Write("签名错误");
            }
            
            return Content("");
        }
    }
}